import { Entity, Column, ManyToOne, JoinColumn } from "typeorm";

import { TonOrder, BIGINT_PG, StockNode, OrderStock, CommonStock, VARCHAR255_PG, ENUM_TON_NOTCH, OrderStockGateway, CommonStockGateway } from "qqlx-core";

import { TransformerBigInteger, TransformerInteger } from "@lib/orm.transfor.number";
import { TransformerVarchar255, TransformerVarchar50 } from "@lib/orm.transfor.string";
import { TransformerEnum } from "@lib/orm.transfor.enum";

import { OwnerBigIdSchema } from "@/alpha/ext.big-id.base";
import { BigIdTradableSchema } from "@/alpha/ext.big-id.tradable";

@Entity({ name: OrderStockGateway.relations })
export class OrderStockSchema extends BigIdTradableSchema implements OrderStock {
  @Column({ transformer: new TransformerVarchar50() })
  key: "count" | "ton" = "count";

  @Column({ transformer: new TransformerEnum(Object.values(ENUM_TON_NOTCH), ENUM_TON_NOTCH.NONE) })
  type: ENUM_TON_NOTCH = ENUM_TON_NOTCH.NONE;

  @Column({ transformer: new TransformerBigInteger(1000) })
  total: BIGINT_PG = "0";

  @ManyToOne("TonOrderSchema", (e: TonOrder) => e.joinOrderStockList)
  @JoinColumn({ name: "tradeId", referencedColumnName: "tradeId" })
  justTonOrder!: TonOrder;
}

@Entity({ name: CommonStockGateway.relations })
export class CommonStockSchema extends OwnerBigIdSchema implements CommonStock {
  @Column({ transformer: new TransformerVarchar50() })
  key: "count" | "ton" = "count";

  @Column({ transformer: new TransformerVarchar255() })
  name: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar255() })
  norm: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar50() })
  unit: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar255() })
  ivid: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerVarchar255() })
  labelStr: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerEnum(Object.values(ENUM_TON_NOTCH), ENUM_TON_NOTCH.NONE) })
  type: ENUM_TON_NOTCH = ENUM_TON_NOTCH.NONE;

  @Column({ transformer: new TransformerBigInteger(1000) })
  total: BIGINT_PG = "0";

  @Column({ transformer: new TransformerInteger() })
  refStockNodeId!: StockNode["id"];

  @ManyToOne("StockNodeSchema", (e: StockNode) => e.joinCommonStockList)
  @JoinColumn({ name: "refStockNodeId", referencedColumnName: "id" })
  justStockNode!: StockNode;
}
